<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>当前开票审核</span>
    </div>
    <searchForm
      :form-data="filter_info"
      :form-title="formTitle"
      @search="search"
      @reset="reset"/>
    <v-tableBtns
      :show-export="true"
      :show-create="true"
      :table-title="exportTitle"
      :table-data="exportData"
      @addNew="addNew"
      :on-export="handleExport"
      title="开票资料"
    />

    <v-table
      :table-title="tableTitle"
      :readable="true"
      :table-data="tableData"
      @sortChange="sortChange"
      :deletable="admin||assistantRole||financeRole"
      btn-class="btn"
      @del="onDelete"
      @edit="edit"
      @read="read"
    />
    <v-pagination
      :page-info="pageInfo"
      :get-table-list="getTableList"
    />

  </el-card>
</template>

<script>
  import {mapGetters} from 'vuex'
  import searchForm from '@/views/components/searchForm'
  import auth from '@/mixins/auth'

  export default {
    name: 'Home',
    components: {searchForm},
    mixins: [auth],
    data() {
      return {
        // f分页
        pageInfo: {
          pageNumber: 1,
          pageSize: 30,
          totalPage: 0,
          totalRow: 0
        },
        // 排序
        sidx: 'reqid',
        sortKey: null,

        formTitle: [
          {
            cn: 'id',
            en: 'id'
          },
          {
            cn: '企业名称: ',
            en: 'customer',
          },
          {
            cn: '负责人查询: ',
            en: 'staffName',
            placeholder: '商务/助理'
          },
          {
            cn: '开票金额: ',
            en: 'money',
            type: 'range',
            dataName: {min: 'moneyMin', max: 'moneyMax'}
          },
          {
            cn: '微赞: ',
            en: 'company_type',
            type: 'select',
            placeholder: '总/分公司',
            list: [
              {
                label: '总公司',
                value: 1
              },
              {
                label: '分公司',
                value: 0
              },
            ]
          },
          {
            cn: '状态: ',
            en: 'checkState',
            type: 'select',
            list: [
              {
                label: '财务退回',
                value: 3
              },
              {
                label: '财务审核',
                value: 4
              },
              {
                label: '开票中',
                value: 5
              },
              {
                label: '待领取',
                value: 6
              }
            ]
          },
          {
            cn: '申请时间: ',
            en: 'created_at',
            type: 'time'
          }
        ],
        filter_info: {},

        tableTitle: [
          {
            cn: 'id',
            en: 'reqid',
            sortable: true
          },
          {
            cn: '企业名称',
            en: 'company_name',
            sortable: true
          },
          {
            cn: '税号',
            en: '纳税人识别号'
          },
          {
            cn: '发票类别',
            en: 'invoice_type',
            sortable: true
          },
          {
            cn: '开票金额',
            en: 'money',
            sortable: true
          },
          {
            cn: '微赞',
            en: 'company_type',
            formatter: (row, column, cellValue, index) => {
              return cellValue ? '总公司' : (cellValue === 0 ? '分公司' : '')
            },
            sortable: true
          },
          {
            cn: '商务',
            en: 'swname',
            sortable: true
          },
          {
            cn: '申请人',
            en: 'create_name',
            sortable: true
          },
          {
            cn: '申请时间',
            en: 'create_date',
            sortable: true
          },
          {
            cn: '状态',
            en: 'cur_status',
            sortable: true
          }
        ],
        tableData: [],

        exportTitle: [
          {
            cn: '创建日期',
            en: 'create_date',
          },
          {
            cn: '企业名称',
            en: '开票公司名称',
          },
          {
            cn: '税号',
            en: '纳税人识别号',
          },
          {
            cn: '公司地址',
            en: '公司地址',
          },
          {
            cn: '电话',
            en: '电话',
          },
          {
            cn: '开户行',
            en: '开户行',
          },
          {
            cn: '帐号',
            en: '帐号',
          },
          {
            cn: '开票金额',
            en: 'money',
          },
          {
            cn: '开票内容',
            en: 'invoice_content',
          },
          {
            cn: '发票类别',
            en: '发票类别',
          },
          {
            cn: '商务',
            en: 'swname',
          },
          {
            cn: '对应订单',
            en: 'mpname',
          },
          {
            cn: '备注',
            en: 'remark',
          },
          {
            cn: '开票状态',
            en: 'cur_status',
          },
        ],
        exportData: [],
      }
    },
    activated() {
      this.getTableList({})
    },
    methods: {

      getTableList(data) {
        const loading = this.$loading()

        const datas = {
          checkState: 8,
          ...this.filter_infos,
          page: this.pageInfo.pageNumber, rows: this.pageInfo.pageSize,
          sidx: this.sidx,
          sortKey: this.sortKey,
        }
        this.$get('action/InvoiceRequisition/dataGrid', datas)
          .then(res => {
            const list = res.data.list.map(item => {
              switch (item.cur_status) {
                case 0:
                  item.cur_status = '商务填写';
                  item.cur_status1 = 0
                  break
                case 1:
                  item.cur_status = '助理退回'
                  item.cur_status1 = 1
                  break
                case 2:
                  item.cur_status = '助理审核'
                  item.cur_status1 = 2
                  break
                case 3:
                  item.cur_status = '财务退回'
                  item.cur_status1 = 3
                  break
                case 4:
                  item.cur_status = '财务审核'
                  item.cur_status1 = 4
                  break
                case 5:
                  item.cur_status = '开票中'
                  item.cur_status1 = 5
                  break
                case 6:
                  item.cur_status = '待领取'
                  item.cur_status1 = 6
                  break
                case 7:
                  item.cur_status = '已完成'
                  item.cur_status1 = 7
                  break
                default :
                  item.cur_status = '财务审核'
                  item.cur_status1 = 4
                  break
              }

              switch (item.invoice_type) {
                case '1':
                  item.invoice_type = '专票'
                  break
                case '2':
                  item.invoice_type = '普票'
                  break
              }
              return item
            })

            this.pageInfo.totalPage = res.data.totalPage
            this.pageInfo.totalRow = res.data.totalRow

            this.tableData = list || [];
              loading.close()
          })
          .catch(_ => {
            loading.close()
          })
      },

      addNew() {
        // 跳转到创建页面
        this.$router.push({
          name: 'invoice_manage.application'

        })
      },

      // 搜索
      search() {
        this.pageInfo.pageNumber=1
        this.getTableList()
      },

      reset() {
        this.filter_info = {}
        this.getTableList()
      },

      editable(val) {
        if (this.admin) {
          return true
        }
        if (this.assistantRole && (val.row.cur_status1 < 4)) {
          return true
        }
        if (!this.financeRole && (val.row.cur_status1 === 1 || val.row.cur_status1 === 3)) {
          return true
        }
        if (this.financeRole && val.row.cur_status1 !== 7) {
          return true
        }
        return false
      },
      edit(val) {
        if (!this.editable(val)) {
          return this.$message({
            message: '你没有权限修改该订单',
            type: 'error'
          })
        }

        this.$router.push({
          name: 'invoice_manage.application',
          query: {
            id: val.row.reqid
          }
        })
      },

      // 查看开票申请
      read(val) {
        this.$router.push({
          name: 'invoice_manage.preview',
          query: {
            id: val.row.reqid
          }
        })
      },

      // 删除开票申请
      onDelete(val) {
        let data
        data = {reqid: val.row.reqid}
        this.$post('action/InvoiceRequisition/del', data)
          .then(res => {
            this.getTableList()
            this.$message({
              message: res.msg,
              type: 'success'
            })
          })
      },

      handleExport() {
        this.$get('action/InvoiceRequisition/GetFile', {type: 0,checkState: 8,  sidx: 'id', ...this.filter_infos})
          .then(res => {
            let datas = res.data;
            let list = datas.map((item) => {
              const obj = item
              switch (obj.cur_status) {
                case  3:
                  obj.cur_status =  '财务退回';
                  break
                case  4:
                  obj.cur_status =  '财务审核';
                  break
                case  5:
                  obj.cur_status =  '开票中';
                  break
                case  6:
                  obj.cur_status =  '待领取';
                  break
                case  7:
                  obj.cur_status =  '已完成';
                  break
                default :
                  break
              }
              switch (obj.invoice_content) {
                case  '1':
                  obj.invoice_content =  '广告费';
                  break
                case  '2':
                  obj.invoice_content =  '广告发布费';
                  break
                case  '3':
                  obj.invoice_content =  '信息服务费';
                  break
                case  '4':
                  obj.invoice_content =  '信息技术服务费';
                  break
                case  '5':
                  obj.invoice_content =  '服务费';
                  break
                default :
                  break
              }
              switch (obj.发票类别) {
                case  '1':
                  obj.发票类别 =  '专票';
                  break
                case  '2':
                  obj.发票类别 =  '普票';
                  break
                default :
                  break
              }
              return obj
            })
            this.exportData = list || []

          }).catch(_ => {
          this.$message({
            message: '导出超时',
            type: 'error'
          })
          this.exportData = []

        })
      },
      // 排序
      sortChange({column, prop, order}) {
        console.log(column, prop, order)
        this.sidx = prop || 'reqid'
        this.sortKey = order === 'ascending' ? 'asc' : null
        this.getTableList()
      }

    },
    computed: {
      ...mapGetters([
        'name',
        'roles'
      ]),
      filter_infos() {
        const data = {}
        const origin = this.filter_info
        for (const key in origin) {
          if (!this.filter_info[key] && this.filter_info[key] !== 0) {
            this.$delete(this.filter_info, key)
            continue
          }
          if (key === 'created_at') {
            data.startDate = origin[key][0]
            data.endDate = origin[key][1]
          } else {
            data[key] = origin[key]
          }
        }
        if (!origin.created_at) {
          delete data.start_time
          delete data.End_time
        }
        return data
      }
    }
  }

</script>

